﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
namespace ForkCarRelationManager.Controls
{
    public partial class ChartControl : UserControl
    {


        dbOperate dbo = new dbOperate();
        ChartBI chartBI = new ChartBI();
        public ChartControl()
        {

            InitializeComponent();
        }

        /*************************************************************初始化按照营业额统计*******************************************/
        private void ChartControl_Load(object sender, EventArgs e)
        {
            ScompanyBI company = new ScompanyBI();
            DataTable tbCompany = company.allCompany();
            CBCompany.DataSource = tbCompany;
            CBCompany.DisplayMember = "ScompanyName";
            CBCompany.ValueMember = "ID";
            CBYear.Text = DateTime.Now.Year.ToString();

            CB2Company.DataSource = tbCompany;
            CB2Company.DisplayMember = "ScompanyName";
            CB2Company.ValueMember = "ID";
            CB2Year.Text = DateTime.Now.Year.ToString();

            this.CBMaintorCompany .DataSource = tbCompany;
            this.CBMaintorCompany.DisplayMember = "ScompanyName";
            this.CBMaintorCompany.ValueMember = "ID";
            this.CBMaintorYear.Text = DateTime.Now.Year.ToString();
            init();

        }

        //大小改变的时候title位置居中
        private void panel2_SizeChanged(object sender, EventArgs e)
        {
            panelTitle.Location = new Point(panel2.Width / 2 - panelTitle.Width / 2, panelTitle.Location.Y);
            panelTitleType.Location = new Point(panel4.Width / 2 - panelTitleType.Width / 2, panelTitleType.Location.Y);
            panel7.Location = new Point(panel5.Width / 2 - panel7.Width / 2, panel7.Location.Y);
        }

        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex = ((TabControl)sender).SelectedIndex;
            if (selectIndex == 1)
            {
                //加载按类型统计界面
                initTypePage();
            }
            else if (selectIndex == 2)
            {
                initMaintorPage();
            }

        }

        private void init()
        {

            if (CBCompany.SelectedValue != null)
            {
                DataTable dt = chartBI.getAyearRecoder(Convert.ToInt32(CBYear.Text), Convert.ToInt32(CBCompany.SelectedValue.ToString()));
                this.dataGridViewMonth.DataSource = dt;


                if (dt.Rows.Count > 0)
                {
                    String month = dt.Rows[0]["month"].ToString();
                    DataTable maintenDT = chartBI.getMaintenRecodeByYearAndMonth(Convert.ToInt32(CBYear.Text), Convert.ToInt32(month), Convert.ToInt32(CBCompany.SelectedValue));
                    this.dataGridViewMenten.DataSource = maintenDT;

                }
                else
                {
                    DataTable tb = (DataTable)this.dataGridViewMenten.DataSource;
                   // tb.Rows.Clear();
                    this.dataGridViewMenten.DataSource = tb;
                }

                this.chart1.DataSource = dt;
                this.chart1.Series[0].YValueMembers = "total";
                this.chart1.Series[0].XValueMember = "month";
                this.chart1.DataBind();

                this.chart2.DataSource = dt;
                this.chart2.Series[0].YValueMembers = "total";
                this.chart2.Series[0].XValueMember = "month";
                this.chart2.DataBind();
            }
        }


        private void dataGridViewMonth_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMonth.CurrentCell.ColumnIndex == 1)
            {
                String month = this.dataGridViewMonth.CurrentRow.Cells["month"].Value.ToString();
                DataTable maintenDT = chartBI.getMaintenRecodeByYearAndMonth(Convert.ToInt32(CBYear.Text), Convert.ToInt32(month), Convert.ToInt32(CBCompany.SelectedValue));
                this.dataGridViewMenten.DataSource = maintenDT;
            }
        }

        private void CBYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            init();
        }
        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void dataGridViewMenten_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMenten.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string maintenID = dataGridViewMenten.CurrentRow.Cells["MaintenID"].Value.ToString();
                MaintenInforForm mdForm = new MaintenInforForm(Convert.ToInt32(maintenID));
                mdForm.ShowDialog();
                if (mdForm.DialogResult == DialogResult.Yes)
                {
                    init();
                }
            }
           
        }

        /***********************************************************按照类型统计***************************************************/

        public void initTypePage()
        {
            //按类型取数据源
            if (CB2Company.SelectedValue != null)
            {
                DataTable dtNei = chartBI.getAyearDataByType(Convert.ToInt32(CB2Year.Text), Convert.ToInt32(CB2Company.SelectedValue.ToString()));

                this.chartType.DataSource = dtNei;
                chartType.Series[0].XValueMember = "month";
                chartType.Series[0].YValueMembers = "baonei";

                chartType.Series[1].XValueMember = "month";
                chartType.Series[1].YValueMembers = "baowai";

                chartType.Series[2].XValueMember = "month";
                chartType.Series[2].YValueMembers = "fangong";
                chartType.DataBind();

                //加载饼图
                DataTable pieData = chartBI.getTotleType(Convert.ToInt32(CB2Year.Text), Convert.ToInt32(CB2Company.SelectedValue.ToString()));
                this.chartTypePie.DataSource = pieData;
                chartTypePie.Series[0].XValueMember = "type";
                chartTypePie.Series[0].YValueMembers = "total";
                chartTypePie.DataBind();
                //按月份列表
                DataTable dt = chartBI.getAyearRecoder(Convert.ToInt32(CB2Year.Text), Convert.ToInt32(CB2Company.SelectedValue.ToString()));
                this.dataGridViewTypeMonth.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    changedrow();
                }
                else
                {
                    DataTable tempTable = (DataTable)this.dataGridViewType.DataSource;
                   // tempTable.Rows.Clear();
                    this.dataGridViewType.DataSource = tempTable;

                    DataTable tempTable2 = (DataTable)dataGridViewMaintenType.DataSource;
                   // tempTable2.Rows.Clear();
                    dataGridViewMaintenType.DataSource = tempTable2;
                }
            }
        }

        private void CB2Year_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl2.SelectedIndex == 1)
            {
                initTypePage();
            }
           
        }

        //查看按钮
        private void dataGridViewMaintenType_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMaintenType.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string maintenID = dataGridViewMaintenType.CurrentRow.Cells["TypeMaintenID"].Value.ToString();
                MaintenInforForm mdForm = new MaintenInforForm(Convert.ToInt32(maintenID));
                mdForm.ShowDialog();
                if (mdForm.DialogResult == DialogResult.Yes)
                {
                    initTypePage();
                }
            }
        }

        //点击月份表联动
        private void dataGridViewTypeMonth_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewTypeMonth.CurrentCell.ColumnIndex == 1)
            {
                changedrow();
            }
        }

        //联动
        public void changedrow()
        {
            String month = this.dataGridViewTypeMonth.CurrentRow.Cells["monthType"].Value.ToString();
            DataTable typetable = chartBI.getTypeDataByMonth(Convert.ToInt32(CB2Year.Text), Convert.ToInt32(CB2Company.SelectedValue.ToString()), Convert.ToInt32(month));
            this.dataGridViewType.DataSource = typetable;

            if (typetable.Rows.Count > 0)
            {
                String typeName = this.dataGridViewType.CurrentRow.Cells["typename"].Value.ToString();
                int type = 0;
                if (typeName == "保内")
                    type = 0;
                else if (typeName == "保外")
                    type = 1;
                else if (typeName == "返工")
                    type = 2;
                String Tyear = this.dataGridViewType.CurrentRow.Cells["Tyear"].Value.ToString();
                String Tmonth = this.dataGridViewType.CurrentRow.Cells["Tmonth"].Value.ToString();
                String TcompantId = this.dataGridViewType.CurrentRow.Cells["TcompanyId"].Value.ToString();
                DataTable typeMaintenTable = chartBI.getMaintenRecodeByYearAndMonth(Convert.ToInt32(Tyear), Convert.ToInt32(Tmonth), Convert.ToInt32(TcompantId), type);
                dataGridViewMaintenType.DataSource = typeMaintenTable;
            }
            else
            {
                DataTable tempTable = (DataTable)dataGridViewMaintenType.DataSource;
                tempTable.Rows.Clear();
                dataGridViewMaintenType.DataSource = tempTable;
            }
        }
        //点击类型表联动
        private void dataGridViewType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewType.CurrentCell.ColumnIndex == 2)
            {
                String typeName = this.dataGridViewType.CurrentRow.Cells["typename"].Value.ToString();
                int type = 0;
                if (typeName == "保内")
                    type = 0;
                else if (typeName == "保外")
                    type = 1;
                else if (typeName == "返工")
                    type = 2;
                String Tyear = this.dataGridViewType.CurrentRow.Cells["Tyear"].Value.ToString();
                String Tmonth = this.dataGridViewType.CurrentRow.Cells["Tmonth"].Value.ToString();
                String TcompantId = this.dataGridViewType.CurrentRow.Cells["TcompanyId"].Value.ToString();
                DataTable typeMaintenTable = chartBI.getMaintenRecodeByYearAndMonth(Convert.ToInt32(Tyear), Convert.ToInt32(Tmonth), Convert.ToInt32(TcompantId), type);
                dataGridViewMaintenType.DataSource = typeMaintenTable;
            }
        }

        /*************************************************************按员工统计*******************************************/

        public void initMaintorPage()
        {
            if (this.CBMaintorCompany.SelectedValue != null)
            {
                DataTable dt = chartBI.getAyearRecoder(Convert.ToInt32(this.CBMaintorYear.Text), Convert.ToInt32(this.CBMaintorCompany.SelectedValue.ToString()));
                this.dataGridViewMaintorYear.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    maintorMaintenClickMonth();
                }
                else
                {
                    DataTable temp = (DataTable)dataGridViewMainterGrade.DataSource;
                   // temp.Rows.Clear();
                    dataGridViewMainterGrade.DataSource = temp;

                    DataTable temp2 = (DataTable)chartMaintorGrade.DataSource;
                    if (temp2 != null)
                    {
                        temp2.Rows.Clear();
                        chartMaintorGrade.DataSource = temp2;
                    }

                    DataTable temp3 = (DataTable)dataGridViewMaintorMainten.DataSource;
                    if (temp3 != null)
                    {
                        temp3.Rows.Clear();
                        dataGridViewMaintorMainten.DataSource = temp3;
                    }

                }
            }
        }

        private void CBMaintorYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl2.SelectedIndex == 2)
            {
                initMaintorPage();
            }
        }

        //查看维修记录
        private void dataGridViewMaintorMainten_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMaintorMainten.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string maintenID = dataGridViewMaintorMainten.CurrentRow.Cells["MaintorMaintenId"].Value.ToString();
                MaintenInforForm mdForm = new MaintenInforForm(Convert.ToInt32(maintenID));
                mdForm.ShowDialog();
                if (mdForm.DialogResult == DialogResult.Yes)
                {
                    initMaintorPage();
                }
            }
        }

        //点击月份联动
        private void dataGridViewMaintorYear_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }
        
        public void maintorMaintenClickMonth()
        {
            //显示一月份的员工成绩
            String month = dataGridViewMaintorYear.CurrentRow.Cells["Mmonth"].Value.ToString();
            DataTable maintorGrade = chartBI.getMaintorGrade(Convert.ToInt32(this.CBMaintorYear.Text), Convert.ToInt32(this.CBMaintorCompany.SelectedValue.ToString()), Convert.ToInt32(month));
            dataGridViewMainterGrade.DataSource = maintorGrade;
            if (maintorGrade.Rows.Count > 0)
            {
                //第一个月的员工业绩图标
                chartMaintorGrade.DataSource = maintorGrade;
                chartMaintorGrade.Series[0].XValueMember = "MaintorName";
                chartMaintorGrade.Series[0].YValueMembers = "Wmoney";

                // chartMaintorGrade.Series[1].XValueMember = "MaintorName";
                 chartMaintorGrade.Series[1].YValueMembers = "Wtime";
                chartMaintorGrade.DataBind();

                //显示第一个员工的维修记录
                String maintorId = dataGridViewMainterGrade.CurrentRow.Cells["MaintorId"].Value.ToString();
                String MaintorYear = dataGridViewMainterGrade.CurrentRow.Cells["MaintorYear"].Value.ToString();
                String MaintorMonth = dataGridViewMainterGrade.CurrentRow.Cells["MaintorMonth"].Value.ToString();
                String MaintorCompany = dataGridViewMainterGrade.CurrentRow.Cells["MaintorCompany"].Value.ToString();

                DataTable MaintorMaintenData = chartBI.getMaintenWithMaintor(Convert.ToInt32(MaintorYear), Convert.ToInt32(MaintorMonth), Convert.ToInt32(MaintorCompany), Convert.ToInt32(maintorId));
                dataGridViewMaintorMainten.DataSource = MaintorMaintenData;
            }
            else
            {
                DataTable temp3 = (DataTable)dataGridViewMaintorMainten.DataSource;
                if (temp3 != null)
                {
                    temp3.Rows.Clear();
                    dataGridViewMaintorMainten.DataSource = temp3;
                }
            }
        }

        //点击维修员联动
        private void dataGridViewMainterGrade_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMainterGrade.CurrentCell.ColumnIndex == 5)
            {
                //显示第一个员工的维修记录
                String maintorId = dataGridViewMainterGrade.CurrentRow.Cells["MaintorId"].Value.ToString();
                String MaintorYear = dataGridViewMainterGrade.CurrentRow.Cells["MaintorYear"].Value.ToString();
                String MaintorMonth = dataGridViewMainterGrade.CurrentRow.Cells["MaintorMonth"].Value.ToString();
                String MaintorCompany = dataGridViewMainterGrade.CurrentRow.Cells["MaintorCompany"].Value.ToString();

                DataTable MaintorMaintenData = chartBI.getMaintenWithMaintor(Convert.ToInt32(MaintorYear), Convert.ToInt32(MaintorMonth), Convert.ToInt32(MaintorCompany), Convert.ToInt32(maintorId));
                dataGridViewMaintorMainten.DataSource = MaintorMaintenData;
            }
        }

        private void dataGridViewMaintorYear_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewMaintorYear.CurrentCell.ColumnIndex == 1)
            {
                maintorMaintenClickMonth();
            }
        }

    }
}
